#!/bin/sh

# Example for $XDG_CONFIG_HOME/sxiv/exec/key-handler
# Called by sxiv(1) after the external prefix key (C-x by default) is pressed.
# The next key combo is passed as its first argument. Passed via stdin are the
# images to act upon, one path per line: all marked images, if in thumbnail
# mode and at least one image has been marked, otherwise the current image.
# sxiv(1) blocks until this script terminates. It then checks which images
# have been modified and reloads them.

# The key combo argument has the following form: "[C-][M-][S-]KEY",
# where C/M/S indicate Ctrl/Meta(Alt)/Shift modifier states and KEY is the X
# keysym as listed in /usr/include/X11/keysymdef.h without the "XK_" prefix.

ALPHA_COLOUR="#352f2f"

readonly KEY="$1";
FNAME="$(xargs -0 echo)"
BNAME="$(basename "$FNAME")"
SDIR="/mnt/toshstor/i/s"
PBDIR="/mnt/toshstor/i/pb"
NTFR=/usr/local/bin/notify-send
EXT=$(file -i "$FNAME" | grep -o "image/.*;" | sed -e 's/image\///' -e 's/;//')

WALLSET_FILL="feh --bg-fill"
WALLSET_TILE="hsetroot -solid $ALPHA_COLOUR -tile"

copyto()
{
	DNAME="$1"
	if [ -e $DNAME/"$BNAME" ]
	then
		cp -T -- "$FNAME" $DNAME/$(date "+%s")_"$BNAME".$EXT
		$NTFR "cp picture dublicate: $DNAME done!" &
	else
		cp -- "$FNAME" $DNAME
		$NTFR "cp picture: $DNAME/$BNAME done!" &
	fi
}

moveto()
{
	DNAME="$1"
	if [ -e $DNAME/"$BNAME" ]
	then
		mv -T -- "$FNAME" $DNAME/$(date "+%s")_"$BNAME".$EXT
		$NTFR "mv picture dublicate: $DNAME done!" &
	else
		mv -- "$FNAME" $DNAME
		$NTFR "mv picture: $DNAME/$BNAME done!" &
	fi
}

deletefile()
{
	rm "$FNAME"
	$NTFR "removing: $FNAME" &
}

wallpf()
{
	WALLPLACE="$HOME/.config/wallpaper"
	cp -f -T "$FNAME" $WALLPLACE
	$WALLSET_FILL $WALLPLACE
	sed -i.back -e 's/solid \\#/solid #/i' -e "s/$WALLSET_TILE/$WALLSET_FILL/i" ~/bin/pathadd/wxrandr.sh
	$NTFR "wallpaper (fill) set!" &
}

wallpt()
{
	WALLPLACE="$HOME/.config/wallpaper"
	cp -f -T "$FNAME" $WALLPLACE
	$WALLSET_TILE $WALLPLACE
	sed -i.back -e "s/$WALLSET_FILL/$WALLSET_TILE/i" -e 's/solid #/solid \\#/i' ~/bin/pathadd/wxrandr.sh
	$NTFR "wallpaper (tiles) set!ss" &
}

help()
{
	sxiv -b ~/.config/sxiv/cheatsheet.png
}


echo "$KEY"

case "$KEY" in
	"F1")           help ;;
	"F4")           copyto $HOME ;;
	"F5")           copyto $PBDIR ;;
	"KP_Enter")     copyto $PBDIR ;;
	"F6")           copyto $PBDIR/dolor ;;
	"KP_Add")       copyto $PBDIR/dolor ;;
	"F7")           copyto $PBDIR/consectetur ;;
	"KP_Subtract")  copyto $PBDIR/consectetur ;;
	"F8")           copyto $HOME/tmp ;;

	"1")            moveto $SDIR/msc ;;
	"KP_1")         moveto $SDIR/msc ;;
	"4")            moveto $SDIR/cat ;;
	"KP_4")         moveto $SDIR/cat ;;
	"5")            moveto $SDIR/inf ;;
	"KP_5")         moveto $SDIR/inf ;;
	"8")            moveto $SDIR/sws ;;
	"KP_8")         moveto $SDIR/sws ;;
	"9")            moveto $SDIR/wh4 ;;
	"KP_9")         moveto $SDIR/wh4 ;;
	"0")            moveto $SDIR/trb ;;
	"c")            moveto $SDIR/wll ;;

	"Z")            wallpf ;;
	"X")            wallpt ;;

	"KP_0")         deletefile ;;
	"Delete")       deletefile ;;
esac
